package com.pur.zsms.matermanage.mater.web;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.pur.zsms.accountmanage.vo.AccountVo;
import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.filter.AttributeListenerUtil;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.ApplicationUtil;
import com.pur.zsms.matermanage.mater.service.IMaterCheckService;
import com.pur.zsms.matermanage.mater.service.IMaterService;
import com.pur.zsms.matermanage.mater.vo.MaterValueVo;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.matermanage.mater.vo.ShoppingCartMaterVo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.shopmanage.service.IShopService;
import com.pur.zsms.supplier.service.ISupplierService;
import com.pur.zsms.systemsetting.datadictionary.service.IDataDictionaryService;

@Controller
@RequestMapping(value = "/materCheck")
public class MaterCheckControllor {

	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(MaterCheckControllor.class);

	protected int page = 1;

	protected int pageSize = 15;

	PaginationSupport pageList;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@Resource(name = "materService")
	private IMaterService materService;

	@Resource(name = "materCheckService")
	private IMaterCheckService materCheckService;

	@Resource(name = "shopService")
	private IShopService shopService;

	@Resource(name = "supplierService")
	private ISupplierService supplierService;

	@Resource(name = "dictionaryService")
	private IDataDictionaryService dataDictionaryService;

	@RequestMapping(value = "/checkMaterCost.do", method = { RequestMethod.POST })
	@ResponseBody
	public String checkMaterCost(HttpSession session, HttpServletRequest request, HttpServletResponse response, int materId, String materValue) {
		JSONObject json = new JSONObject();
		UserPo userInfo = (UserPo) session.getAttribute("user");
		boolean flag = true;
		try {
			int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
			flag = materCheckService.checkMaterCost(supId, materId, materValue);
		} catch (GenericException e) {
			e.printStackTrace();
			json.put("success", false);
			return json.toString();
		}
		if (!flag) {
			json.put("msg", materCheckService.getMsg());
			json.put("success", false);
		} else {
			json.put("success", true);
		}
		return json.toString();
	}

	@RequestMapping(value = "/checkMaterSale.do", method = { RequestMethod.POST })
	@ResponseBody
	public String checkMaterSale(HttpSession session, HttpServletRequest request, HttpServletResponse response, int materId, String materValue) {
		JSONObject json = new JSONObject();
		UserPo userInfo = (UserPo) session.getAttribute("user");
		boolean flag = true;
		try {
			int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
			flag = materCheckService.checkMaterSale(supId, materId, materValue);
		} catch (GenericException e) {
			e.printStackTrace();
			json.put("success", false);
			return json.toString();
		}
		if (!flag) {
			json.put("msg", materCheckService.getMsg());
			json.put("success", false);
		} else {
			json.put("success", true);
		}
		return json.toString();
	}
	
	@RequestMapping(value = "/checkMaterUnitChange.do", method = { RequestMethod.POST })
	@ResponseBody
	public String checkMaterUnitChange(HttpSession session, HttpServletRequest request, HttpServletResponse response, int materId) {
		JSONObject json = new JSONObject();
		UserPo userInfo = (UserPo) session.getAttribute("user");
		boolean flag = true;
		try {
			int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
			flag = materCheckService.checkMaterUnitChange(supId, materId);
		} catch (GenericException e) {
			e.printStackTrace();
			json.put("success", false);
			return json.toString();
		}
		if (!flag) {
			json.put("msg", materCheckService.getMsg());
			json.put("success", false);
		} else {
			json.put("success", true);
		}
		return json.toString();
	}
}
